﻿/*
 * =====================================================================================
 *
 *       Filename:  DataConvert
 *
 *    Description:  数据转换类
 *
 *        Version:  1.0
 *        Created:  
 *       Revision:  none
 *       Compiler:  
 *
 *         Author:   imbedinlove
 *        Company:  
 *       EditTime:  2014年5月22日 17:02:42 
 *
 * =====================================================================================

 */

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace ImbedLibrary
{
    public class DataConvert
    {

        /// <summary>
        /// 转换数据
        /// </summary>
        /// <param name="list">数据</param>
        /// <returns></returns>
        public static DataTable ConvertListToDataTable(IList<ArrayList> list)
        {

            DataTable table = new DataTable();
            DataColumn column;
            DataRow row;

            for (int i = 0; i < list[0].Count; i++)
            {
                column = new DataColumn();
                column.DataType = list[0][i].GetType();
                column.ColumnName = i.ToString();
                table.Columns.Add(column);
            }

            for (int i = 0; i < list.Count; i++)
            {
                row = table.NewRow();
                for (int n = 0; n < list[i].Count; n++)
                {
                    row[n] = list[i][n];
                }
                table.Rows.Add(row);
            }

            return table;
        }

        /// <summary>
        /// Array转换List<string>
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="isLower">是否转换小写</param>
        /// <returns></returns>
        public static List<string> ArrayToList(string[] data, bool isLower = false)
        {
            List<string> result = new List<string>();
            foreach (var item in data)
            {
                if (isLower)
                {
                    result.Add(item.ToLower());
                }
                else
                {
                    result.Add(item);
                }
            }
            return result;
        }
    }
}
